import 'package:flutter/widgets.dart';
import 'package:myturngame/Theme.dart';

import '../bean/message.dart';

///信息窗口
class MessageWindow extends StatefulWidget {
  final List<Message> messageList;

  const MessageWindow(this.messageList,{Key? key}) : super(key: key);

  @override
  State<MessageWindow> createState() => _MessageWindowState();
}

class _MessageWindowState extends State<MessageWindow> {

  final ScrollController _controller = ScrollController();

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _controller.jumpTo(_controller.position.maxScrollExtent);
    });
  }

  @override
  void didUpdateWidget(covariant MessageWindow oldWidget) {
    super.didUpdateWidget(oldWidget);
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _controller.jumpTo(_controller.position.maxScrollExtent);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 150,
      height: 200,
      padding: const EdgeInsets.all(5),
      decoration: BoxDecoration(
        color: const Color(0x80000000),
        borderRadius: BorderRadius.circular(5),
      ),
      child: ListView.builder(
        controller: _controller,
        itemCount: widget.messageList.length,
        itemBuilder: ((context, index) {
          return Text.rich(TextSpan(children: widget.messageList[index].textSpans));
        }),
      ),
    );
  }
}
